from langdev_helper.llm.lcex import llm_lcex as llm

from langgraph.graph import StateGraph, START, END
from langchain_core.tools import tool
from langchain_community.tools.file_management import ReadFileTool, ListDirectoryTool, FileSearchTool, WriteFileTool
from langgraph.prebuilt import ToolNode, tools_condition

from state import State

graph_builder = StateGraph(State)

tools = [ReadFileTool(), ListDirectoryTool(), FileSearchTool(), WriteFileTool()]
llm_with_tools = llm.bind_tools(tools)

def chatbot(state: State):
    return {"messages": [llm_with_tools.invoke(state["messages"])]}

graph_builder.add_node("chatbot", chatbot)

tool_node = ToolNode(tools=tools)
graph_builder.add_node("tools", tool_node)

graph_builder.add_conditional_edges(
    "chatbot",
    tools_condition,
)
graph_builder.add_edge("tools", "chatbot")
graph_builder.add_edge(START, "chatbot")

graph = graph_builder.compile()

